package com.yc.controller;


import com.yc.entity.User;
import com.yc.service.IUserService;
import com.yc.service.ServiceException;
import com.yc.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * InnoDB free: 5120 kB 前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-05-08
 */
@Controller
@RequestMapping("//user")
public class UserController {
    @Resource
    private IUserService userService;

    @PostMapping("login")
    @ResponseBody
    public Result login(@RequestBody User user) {
        try {
            User user1 = userService.login(user);
            return new Result(1, "登录成功", user1);
        } catch ( ServiceException e) {
            e.printStackTrace();
            return new Result(0, e.getMessage(), null);
        }
    }

    @GetMapping("getLoginedUser")
    @ResponseBody
    public Result getLoginedUser(@SessionAttribute(required = false) User loginedUser,
                                 HttpServletRequest httpServletRequest) {
        if (loginedUser == null) {
            return new Result(0, "尚未登录!"+httpServletRequest.getServerPort(), null);
        } else {
            return new Result(1, "登录成功"+httpServletRequest.getServerPort(), loginedUser);
        }
    }
}

